/*****************************************************
PROJECT: BRIDGE C2 SCRAP
JOB:
REQUESTOR:Rachel Gold
JIRA #:
ANALYST: Jean O'Malley
DATE: 8/4/2022

 
LAST UPDATED: 10/3/2023 Created new combined vulnerability variable for analysis of UTD at index visit and months UTD:
Any vulnerabilty vs none for ptients screened for any social needs

PEER REVIEWER: 

PROJECT DESCRIPTION: Use ASCEND approach to compare CRC, CVC and 
BC cancer prevention services in patients with
	No SDH Screening
	Documented Need
	Documented No Need
Stratified by SDH need: Food, Housing, Transportation
STUDY PERIOD: 7/1/2016-2/29/2020		

DETAILS (this file): 
CRC Outcome variables by SDH
	UTD at index 
	Months UTD over observation period


INPUT DATA:
SCRAP.CRC_analysis_09062023;


SECTIONS:
A. Dynamic variables and analysis fileB. Full cohort variables
	B1. UTD at index 
	B3. Months UTD over observation period



****************************************************/
options compress=yes reuse=yes spool;
libname SCRAP "e:\\sasroot\BRIDGE C2\SCRAP\";
libname RD odbc noprompt="dsn=research_dev;Trusted_Connection=yes" bulkload=yes;

/****************************************************/
/***************************************************
A. Dynamic variables and analysis file
***************************************************/
%let cancer=CVC;
%Let covariates= race_ethnic2 language_c  FPL_cat2 age_cat payor_type_research visit_cat;
%Let class=delivery_site_id  age_cat race_ethnic2 language_c  FPL_cat2 visit_cat payor_type_research;
%let SDH=combined_screen; 



data scrap.&cancer._analysis_09122023;
set SCRAP.&cancer._analysis_09122023;
length combined_screen $20;
any_screen=(sdh_food^='3_Never Screened') and (sdh_housing^='3_Never Screened')
and (sdh_transportation^='3_Never Screened');
any_positive=max((sdh_food='1_Positive Screen'), (sdh_housing='1_Positive Screen'),
(sdh_transportation='1_Positive Screen'));
if any_screen=0 then combined_screen='3_Never Screened';
if any_screen=1 and any_positive=0 then combined_screen='2_Negative Screen';
if any_positive=1 then combined_screen='1_Positive Screen';
run;


data analysis;
set SCRAP.&cancer._analysis_09122023;
run;


proc sort data=analysis;
by delivery_site_id;
run;


/************************************************
B. Full cohort variables
************************************************/
/*	B1. UTD at index */


	proc genmod data=analysis;
	class &class &sdh /ref=first;
	model due=&sdh &covariates/link=log d=binomial type3;
	repeated subject=delivery_site_id/type=exch;
	LSmeans &sdh/ilink CL om;
	ods output LSmeans=due_&sdh lsmestimates=due_diffs_&sdh;
	lsmestimate &sdh '1 vs 2' -1 0 1/cl  om ilink;
	lsmestimate &sdh '3 vs 2' -1 1 0 /cl   om ilink;
	run;

	proc sort data=due_&sdh out=var1_&sdh (keep=&sdh Mu LowerMu UpperMu);
	by &sdh;
	run;
	data var1_&sdh(keep=Outcome &sdh prob_CI);
	set var1_&sdh;
	length Outcome $30;
	format mu2 lower2 upper2 4.1;
	Outcome="Up to Date &cancer screening";
	mu2=round(MU*100,0.1);
	mu_txt=put(mu2,4.1); 
	lower2=round(lowerMU*100,0.1);
	lower_txt=put(lower2,4.1);
	upper2=round(upperMU*100,0.1);
	upper_txt=put(upper2,4.1);
	prob_CI=cat(mu_txt,' (',lower_txt,', ',upper_txt,')');
	run;
	proc sort data=due_diffs_&sdh out=diff1_&sdh (keep= Probz label Mu Lowermu Uppermu);
	by label;
	run;

	data diff1_&sdh (keep=Outcome &sdh reference RR_CI probz);
	set diff1_&sdh;
	length Outcome $30 &sdh reference $20;
	format mu2 lower2 upper2 4.2;
	Outcome="Up to Date &cancer screening";
	reference='2_Negative Screen';
	if label='1 vs 2' then &SDH= '1_Positive Screen';
	if label='3 vs 2' then &SDH= '3_Never Screened';
	mu2=round(MU,0.01);
	mu_txt=put(mu2,4.2); 
	lower2=round(lowerMU,0.01);
	lower_txt=put(lower2,4.2);
	upper2=round(upperMU,0.01);
	upper_txt=put(upper2,4.2);
	RR_CI=cat(mu_txt,' (',lower_txt,', ',upper_txt,')');
	run;

	data var1_&sdh;
	merge var1_&sdh diff1_&sdh;
	by outcome &sdh;
	run;


	proc datasets library=work;
	delete due_diffs_&sdh diff1_&sdh  due_&sdh;
	quit;




/*	B3. Months UTD over observation period*/

	proc genmod data=analysis ;
	class &class &sdh /ref=first;
	model months_utd=&sdh &covariates/link=log d=negbin type3 offset=months_offset;
	repeated subject=delivery_site_id/type=exch;
	LSmeans &sdh/ilink CL OM;
	ods output LSmeans=mUTD_&sdh lsmestimates=mUTD_diffs_&sdh;
	lsmestimate &sdh '1 vs 2' -1 0 1/cl  om ilink;
	lsmestimate &sdh '3 vs 2' -1 1 0 /cl   om ilink;
	run;

	proc sort data=mUTD_&sdh out=var3_&sdh (keep=&sdh Mu LowerMu UpperMu);
	by &sdh;
	run;
	data var3_&sdh(keep=Outcome &sdh prob_CI);
	set var3_&sdh;
	length Outcome $30;
	Outcome="Percent Months UTD";
	format mu2 lower2 upper2 4.1;
	mu2=round(MU*100,0.1);
	mu_txt=put(mu2,4.1); 
	lower2=round(lowerMU*100,0.1);
	lower_txt=put(lower2,4.1);
	upper2=round(upperMU*100,0.1);
	upper_txt=put(upper2,4.1);
	prob_CI=cat(mu_txt,' (',lower_txt,', ',upper_txt,')');
	run;
	proc sort data=mUTD_diffs_&sdh out=diff3_&sdh (keep= Probz label mu Lowermu Uppermu);
	by label;
	run;

	data diff3_&sdh (keep=Outcome &sdh reference rr_CI probz);
	set diff3_&sdh;
	length Outcome $30 &sdh reference $20;
	format mu2 lowermu uppermu 4.2;
	Outcome="Percent Months UTD";
	reference='2_Negative Screen';
	if label='1 vs 2' then &SDH= '1_Positive Screen';
	if label='3 vs 2' then &SDH= '3_Never Screened';

	mu2=round(MU,0.01);
	mu_txt=put(mu2,4.2); 
	lower2=round(lowerMU,0.01);
	lower_txt=put(lower2,4.2);
	upper2=round(upperMU,0.01);
	upper_txt=put(upper2,4.2);
	RR_CI=cat(mu_txt,' (',lower_txt,', ',upper_txt,')');
	run;
	data var3_&sdh;
	merge var3_&sdh diff3_&sdh;
	by outcome &sdh;
	run;


	proc datasets library=work;
	delete mUTD_diffs_&sdh diff3_&sdh mUTD_diffs_&sdh mUTD_&sdh;
	quit;

	proc freq data=analysis;
	tables combined_screen;
	run;